# -*- coding:utf-8 -*-
#先引入后面可能用到的library
from StockTushare import StockTushare
from SNetwork.StockRequest import StockRequest
from STool.StockTool import StockTool

from SDaily.StockSingle import StockSingle
#先引入后面可能用到的library
import tushare as ts
import datetime
import time
from pymongo import MongoClient
from threading import Thread
from multiprocessing import Process

import pandas as pd
from stock_py.SysFile import Base_File_Oper

ts.set_token(Base_File_Oper.read_tushare_token())
pro = ts.pro_api()
today_mt_string= datetime.datetime.now().strftime('%Y%m%d')

def stock_daily_update(all_stock_count,singleObjc,stocks):

    tushareObj=StockTushare()
    size_array_stosks =StockTool.list_split(stocks,150)
    tushareObj.updateStocks(size_array_stosks,all_stock_count,singleObjc)
    
    
def stock_BK_update():
    
    tushareObj=StockTushare()
    tushareObj.updateBK()
    
    
    
def stock_limit_update():
    tushareObj=StockTushare()
    return tushareObj.stock_limit_update()
    
def stock_Zhishu_update():
    tushareObj=StockTushare()
    tushareObj.updateZhishu()
    

if __name__ == '__main__':
    # 更新本地数据库
    # 1、获取最新股票库和交易数据
    
    # stock_Zhishu_update()
        
    df = pro.stock_basic(exchange='',fields='ts_code,symbol,name,industry,list_date,list_status')
    df = df.rename(columns={'industry': 'hy'})

    all_stocks = df.to_dict('records')
    print("所有股票数 %s" %(len(all_stocks)))
  
    singleObjc = StockSingle()
    
    start_time_0 = time.time()
    
    all_stocks = [item for item in all_stocks if item['list_date'] <= today_mt_string]
    print(("最新交易日期 ----- %s  股票数量： %s 个" %(today_mt_string,len(all_stocks))))
    all_stock_count = len(all_stocks)
    index_stock_count = 0

    size_array_stosks =StockTool.list_split(all_stocks, 240)
    for index,stocks in enumerate(size_array_stosks):
        stock_daily_update(all_stock_count,singleObjc,stocks)

    stop_time_0 = time.time()
    cost_0 = stop_time_0 - start_time_0

    #涨跌停跟踪
    # stock_limit_update()

    a = StockSingle()
    if "20" in a.trade_date_now:
        stock_BK_update()

    